package com.hcinfos.gsoo.adapter;

import java.util.List;

import com.hcinfos.gsoo.GsooApplication;
import com.hcinfos.gsoo.R;
import com.hcinfos.gsoo.enity.GdMealsDish;
import com.hcinfos.gsoo.interfaze.DataCallback;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.bitmap.BitmapDisplayConfig;
import com.lidroid.xutils.db.sqlite.CursorUtils.FindCacheSequence;

import android.content.Context;
import android.os.Parcelable;
import android.provider.ContactsContract.CommonDataKinds.Relation;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * 
 * @author xi
 * 
 */
public class MenuScrollViewAdapter extends PagerAdapter {

	private LayoutInflater inflater;
	private Context context;
	private List<GdMealsDish> list;
	private DataCallback callback;
	public DataCallback call;
	private BitmapUtils bitmapUtils;
	private BitmapDisplayConfig bigPicDisplayConfig;

	public MenuScrollViewAdapter(List<GdMealsDish> list, Context context,
			DataCallback callback) {
		this.context = context;
		this.list = list;
		inflater = LayoutInflater.from(context);
		this.callback = callback;
		bitmapUtils = new BitmapUtils(context);
		bigPicDisplayConfig = new BitmapDisplayConfig();
		bigPicDisplayConfig.setLoadFailedDrawable(context.getResources()
				.getDrawable(R.drawable.tc_shop_default_logo));
		bigPicDisplayConfig.setLoadingDrawable(context.getResources()
				.getDrawable(R.drawable.tc_shop_default_logo));
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public boolean isViewFromObject(View arg0, Object arg1) {
		return arg0 == arg1;
	}

	@Override
	public int getItemPosition(Object object) {
		return POSITION_NONE;
	}

	@Override
	public Object instantiateItem(ViewGroup container, int position) {
		View imageLayout = inflater.inflate(R.layout.scroll_menu_detail_view,
				container, false);
		final RelativeLayout oper_rl = (RelativeLayout) imageLayout
				.findViewById(R.id.oper_rl);
		TextView scrollName_tv = (TextView) imageLayout
				.findViewById(R.id.scroll_name_tv);
		final TextView add_tip_tv = (TextView) imageLayout
				.findViewById(R.id.add_tip_tv);
		final TextView detail_num_tv = (TextView) imageLayout
				.findViewById(R.id.detail_num_tv);
		final GdMealsDish dish = list.get(position);
		scrollName_tv.setText(dish.getDishName());
		ImageView imageView = (ImageView) imageLayout
				.findViewById(R.id.imageView);
		TextView price_tv = (TextView) imageLayout.findViewById(R.id.price_tv);
		TextView intro_tv = (TextView) imageLayout.findViewById(R.id.intro_tv);

		price_tv.setText("¥" + dish.getDishPrice());
		intro_tv.setText(dish.getDishDes());
		ImageView close_iv = (ImageView) imageLayout
				.findViewById(R.id.close_iv);
		((ViewPager) container).addView(imageLayout);
		close_iv.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				if (callback != null)
					callback.callback(1);

			}
		});
		if (GsooApplication.menuMap.get(dish.getId()) != null) {
			oper_rl.setVisibility(View.VISIBLE);
			add_tip_tv.setVisibility(View.GONE);
			detail_num_tv.setText(GsooApplication.menuMap.get(dish.getId())
					.getNum() + "");
		} else {
			oper_rl.setVisibility(View.GONE);
			add_tip_tv.setVisibility(View.VISIBLE);
		}
		ImageView add_iv = (ImageView) imageLayout.findViewById(R.id.add_iv);
		ImageView sub_iv = (ImageView) imageLayout.findViewById(R.id.sub_iv);
		add_iv.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				GdMealsDish extend = GsooApplication.menuMap.get(dish.getId());
				int num = extend.getNum();
				num += 1;
				extend.setNum(num);
				detail_num_tv.setText(num + "");
				if (call != null)
					call.callback(0);
			}
		});
		sub_iv.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				GdMealsDish extend = GsooApplication.menuMap.get(dish.getId());
				if (extend != null) {
					int num = extend.getNum();
					num -= 1;
					if (num <= 0) {
						num = 0;
						GsooApplication.menuMap.remove(dish.getId());
						oper_rl.setVisibility(View.GONE);
						add_tip_tv.setVisibility(View.VISIBLE);
					}
					extend.setNum(num);
					detail_num_tv.setText(num + "");
				}
				if (call != null)
					call.callback(0);
			}
		});
		add_tip_tv.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				GdMealsDish extend = dish;
				extend.setNum(1);
				oper_rl.setVisibility(View.VISIBLE);
				add_tip_tv.setVisibility(View.GONE);
				GsooApplication.menuMap.put(dish.getId(), extend);
				detail_num_tv.setText("1");
				if (call != null)
					call.callback(0);
			}
		});
		bitmapUtils.display(imageView, dish.getImgAddr1(), bigPicDisplayConfig);
		return imageLayout;
	}

	@Override
	public void restoreState(Parcelable arg0, ClassLoader arg1) {

	}

	@Override
	public Parcelable saveState() {
		return null;
	}

	@Override
	public void startUpdate(View arg0) {

	}

	@Override
	public void finishUpdate(View arg0) {

	}

	@Override
	public void destroyItem(View container, int position, Object object) {
		((ViewPager) container).removeView((View) object);
	}

	public void setDataChange(List<GdMealsDish> list) {

		this.list = list;
		notifyDataSetChanged();
	}

	public void setDataCallback(DataCallback callback) {
		this.call = callback;
	}
}